package com.lanlian.cayman.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.TimeCount;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqSendSmsCode;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.uikit.VerificationCodeInput;
import com.lanlian.utillib.DisplayUtil;
import com.lanlian.utillib.ToastUtils;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/5 16:35
 *     desc   : 认证手机验证码对话框
 *     version: 1.0
 * </pre>
 */
public class RechargeVerifyMobileDialog extends Dialog implements View.OnClickListener {

    private Context mContext;
    private ImageView mImgBack;
    private ImageView mImgClose;
    private TextView mTvTime;
    private VerificationCodeInput mTvCode;

    private TimeCount mTimeCount;
    private OnCompleteListener mOnCompleteListener;

    public RechargeVerifyMobileDialog(@NonNull Context context, OnCompleteListener onCompleteListener) {
        super(context, R.style.customDialog);
        this.mContext = context;
        this.mOnCompleteListener = onCompleteListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_verify_recharge);
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (DisplayUtil.getScreenWidth(mContext) * 0.8);
        window.setAttributes(lp);
        initView();
        setListeners();
        mTvCode.setOnCompleteListener(new VerificationCodeInput.Listener() {
            @Override
            public void onComplete(String content) {
                mOnCompleteListener.onComplete(RechargeVerifyMobileDialog.this, content);
            }
        });
        setCanceledOnTouchOutside(false);
    }

    private void initView() {
        mImgBack = findViewById(R.id.img_back);
        mImgClose = findViewById(R.id.img_close);
        mTvTime = findViewById(R.id.tv_time);
        mTvCode = findViewById(R.id.tv_code);
        mTimeCount = new TimeCount(mContext, 60000, 1000, mTvTime);
        mTimeCount.startTime();
        mTvTime.setClickable(false);
    }

    private void setListeners() {
        mImgClose.setOnClickListener(this);
        mImgBack.setOnClickListener(this);
        mTvTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                dismiss();
                break;
            case R.id.img_close:
                dismiss();
                break;
            case R.id.tv_time:
                mTvTime.setClickable(false);
                VolleyRequestUtils.getInstance().sendSmsCode(this.toString(), new ReqSendSmsCode(),
                        new INetWorkCallback() {
                            @Override
                            public void onSuccess(Object response) {
                                mTimeCount.startTime();
                                ToastUtils.show(mContext, "验证码已发送至您的手机");
                            }

                            @Override
                            public void onFail(BaseResponse baseResponse) {
                                mTvTime.setClickable(true);
                                ToastUtils.show(mContext, baseResponse.getMsg());
                            }
                        });
                break;
            default:
                break;
        }
    }

    public interface OnCompleteListener {
        void onComplete(RechargeVerifyMobileDialog verifyMobileDialog, String content);
    }

    @Override
    public void show() {
        super.show();
        mTvCode.focus();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showInput();
            }
        }, 100);
    }

    public void clear() {
        mTvCode.clear();
    }

    private void showInput() {
        InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(mTvCode.getEditText(0), InputMethodManager.SHOW_IMPLICIT);
    }

    public void hideInput() {
        InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(mTvCode.getEditText(0).getWindowToken(), 0);
    }

}
